home *** CD-ROM | disk | FTP | other *** search
/ Aminet 21 / Aminet 21 (1997)(GTI - Schatztruhe)[!][Oct 1997].iso / Aminet / dev / misc / gms_dev.lha / GMS / Source / C / 3DObjects / Helix.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-07-09  |  4.0 KB  |  162 lines

  1. /*
  2. ** Name:      Spinning Helix
  3. ** Version:   1.0
  4. ** Author:    Paul Manias
  5. ** Copyright: DreamWorld Productions (c) 1997
  6. ** SAS/C:     sc Helix.c
  7. **
  8. ** Doc:       This is a demo of a spinning helix consisting entirely of dots.
  9. **            The object is pre-calculated then rotated in real time for speed,
  10. **            although things could be faster than this.  Move the mouse to scale
  11. **            the object to any size.
  12. **
  13. */
  14.  
  15. #include <proto/games.h>
  16. #include <math.h>
  17.  
  18. extern struct GMSBase *GMSBase;
  19. APTR   PREFSNAME = DEFAULT;
  20.  
  21. struct GameScreen *screen;
  22.  
  23. void Demo(void);
  24.  
  25. struct DotPixel { double X,Y,Z; };
  26.  
  27. #define AMTCOLOURS 32
  28.  
  29. ULONG palette[AMTCOLOURS] = {
  30.   0x000000,0x101010,0x171717,0x202020,0x272727,0x303030,0x373737,0x404040,
  31.   0x474747,0x505050,0x575757,0x606060,0x676767,0x707070,0x777777,0x808080,
  32.   0x878787,0x909090,0x979797,0xa0a0a0,0xa7a7a7,0xb0b0b0,0xb7b7b7,0xc0c0c0,
  33.   0xc7c7c7,0xd0d0d0,0xd7d7d7,0xe0e0e0,0xe0e0e0,0xf0f0f0,0xf7f7f7,0xffffff
  34. };
  35.  
  36. /***********************************************************************************/
  37.  
  38. void main(void)
  39. {
  40.   if (screen = AddScreenTags(TAGS_GAMESCREEN,NULL,
  41.      GSA_AmtColours, AMTCOLOURS,
  42.      GSA_Palette,    palette,
  43.      GSA_Attrib,     DBLBUFFER,
  44.      TAGEND)) {
  45.  
  46.      ShowScreen(screen);
  47.  
  48.      Demo();
  49.  
  50.   DeleteScreen(screen);
  51.   }
  52. }
  53.  
  54. /************************************************************************************
  55. ** Longtitude deterimines the position of the dot on the horizontal axis.
  56. ** Latitude determines the position of the dot on the vertical axis.
  57. */
  58.  
  59. #define AMTDOTS 500       /* The amount of dots in the Object. */
  60. #define MAXZ    6.3926    /* MAXZ  */
  61.  
  62. void Demo(void)
  63. {
  64.   struct DotPixel *object;
  65.   WORD   i;
  66.   WORD   offsetx = (screen->ScrWidth/2);
  67.   WORD   offsety = (screen->ScrHeight/2);
  68.   double temp;
  69.   double angle=0;
  70.   ULONG  colour;
  71.   double Z2,X2,Y2;
  72.   ULONG  jport1;
  73.   LONG   scale=16;
  74.   UWORD  anglex=0, angley=0, anglez=0;
  75.   double u,v;
  76.   double *sine;       /* Pointer to our sine table */
  77.   double *cosine;     /* Pointer to our cosine table */
  78.  
  79.   object = AllocMemBlock(sizeof(struct DotPixel)*AMTDOTS,MEM_ANY);
  80.   sine   = AllocMemBlock(sizeof(double)*360,MEM_ANY);
  81.   cosine = AllocMemBlock(sizeof(double)*360,MEM_ANY);
  82.  
  83.   /* First calculate the X, Y and Z coordinates of our object. */
  84.  
  85.   for (i=0; i<AMTDOTS; i++) {
  86.     u = ((double)FastRandom(12566-1)+1)/1000;  /*  0 < u < 4*PI  */
  87.     v = ((double)FastRandom(6283-1)+1)/1000;   /*  0 < v < 2*PI  */
  88.     object[i].X  = cos(u)*(2+cos(v));
  89.     object[i].Y  = sin(u)*(2+cos(v));
  90.     object[i].Z  = (u-2*3.14159)+sin(v);
  91.   }
  92.  
  93.   /* Now generate our cosine and sinus tables */
  94.  
  95.   for (i=0; i<360; i++) {
  96.     cosine[i] = cos(angle);
  97.     sine[i]   = sin(angle);
  98.     angle    += 0.25;
  99.   }
  100.  
  101.   /* Go into our main loop */
  102.  
  103.   InitJoyPorts();
  104.  
  105.   do
  106.   {
  107.     jport1 = ReadJoyPort(JPORT1,JT_ZBXY);
  108.     scale += GetY(jport1);
  109.     if (scale < 1) scale = 1;
  110.     if (scale > 100) scale = 100;
  111.  
  112.     ClearBitmap(screen->Bitmap);
  113.  
  114.     for (i=0; i<AMTDOTS; i++) {
  115.  
  116.       X2 = object[i].X;
  117.       Y2 = object[i].Y;
  118.       Z2 = object[i].Z;
  119.  
  120.       /* Rotate the X axis */
  121.  
  122.       temp = Z2;
  123.       Z2 = Z2*cosine[anglex] - Y2*sine[anglex];
  124.       Y2 = Y2*cosine[anglex] + temp*sine[anglex];
  125.  
  126.       /* Rotate the Y axis */
  127.  
  128.       temp = Z2;
  129.       Z2 = Z2*cosine[angley] - X2*sine[angley];
  130.       X2 = X2*cosine[angley] + temp*sine[angley];
  131.  
  132.       /* Rotate the Z axis */
  133.  
  134. //      temp = X2;
  135. //      X2 = X2*cosine[anglez] - Y2*sine[anglez];
  136. //      Y2 = Y2*cosine[anglez] + temp*sine[anglez];
  137.  
  138.       /* Calculate colour based on Z position (-1.96 < Z < +1.96) */
  139.  
  140.       colour = (((Z2+MAXZ)/MAXZ)*screen->AmtColours)/2;
  141.  
  142.       /* Finally scale the (x,y) coordinates to enlarge or shrink the sphere */
  143.  
  144.       X2 *= scale;
  145.       Y2 *= scale;
  146.  
  147.       DrawPixel(screen->Bitmap,(WORD)X2+offsetx,(WORD)Y2+offsety,colour);
  148.     }
  149.     anglex++; if (anglex >= 360) anglex = 0;
  150.     angley++; if (angley >= 360) angley = 0;
  151.     anglez++; if (anglez >= 360) anglez = 0;
  152.  
  153.     WaitVBL();
  154.     SwapBuffers(screen);
  155.   } while(!(jport1 & MB_LMB));
  156.  
  157.   FreeMemBlock(object);
  158.   FreeMemBlock(sine);
  159.   FreeMemBlock(cosine);
  160. }
  161.  
  162.